package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpMapper;
import com.itheima.mapper.OperateLogMapper;
import com.itheima.pojo.JobOption;
import com.itheima.pojo.OperateLog;
import com.itheima.pojo.PageResult;
import com.itheima.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private OperateLogMapper operateLogMapper;

    //方法一：返回实体对象
    @Override
    public JobOption getEmpJobData() {
        List<Map<String,Object>> list = empMapper.countEmpJobData();
        List<Object> jobList = list.stream().map(dataMap -> dataMap.get("pos")).toList();
        List<Object> dataList = list.stream().map(dataMap -> dataMap.get("total")).toList();
        return new JobOption(jobList,dataList);
    }

    @Override
    public List<Map> getEmpGenderData() {
        List<Map> genderList = empMapper.countEmpGenderData();
        return genderList;
    }

    @Override
    public List<Map> getStudentDegreeData() {
        List<Map> degreeList = empMapper.countStudentDegreeData();
        return degreeList;
    }

    //方法二：直接返回Map集合
    @Override
    public Map getStudentCountData() {
        List<Map> studentCountData = empMapper.getStudentCountData();
        List<Object> clazzList = studentCountData.stream().map(clazz -> clazz.get("clazz")).toList();
        List<Object> dataList = studentCountData.stream().map(data -> data.get("data")).toList();

        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("clazzList", clazzList);  // 键名与JSON中的clazzList对应
        dataMap.put("dataList", dataList);

        return dataMap;
    }

    @Override
    public PageResult getLogPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        //这里的List必须指定泛型，因为PageHelper需要在执行查询前就知道返回类型
        List<OperateLog> logPage = operateLogMapper.getLogPage(page, pageSize);
        Page p=(Page) logPage;
        return new PageResult<>(p.getTotal(),p.getResult());
    }
}
